const { Service } = require('egg');
const { tableToList } = require('../utils/tools');


module.exports = class CourseService extends Service {
  /**
   * * 学生个人课表查询
   * @param {string} term 学期
   * @return {Object} *
   */
  async table(term) {
    const Cookie = this.ctx.state.Cookie;
    const apiUrl = `${this.ctx.state.baseUrl}/jsxsd/xskb/xskb_list.do`;
    const res = await this.ctx.curl(apiUrl, {
      method: 'POST',
      headers: {
        Cookie,
        'Content-Type': 'application/x-www-form-urlencoded',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
      },
      data: {
        cj0701id: '',
        zc: '',
        demo: '',
        xnxq01id: term,
        sfFD: '1',
        kbjcmsid: '94673FF3230E4769E0533C41FF0A2703',
      },
      timeout: 10000,
    });
    if (res.status === 200) {
      try {
        const tableList = tableToList(res.data.toString(), '#kbtable');
        const time = []; // 记录每节课上课时间
        const note = ''; // 备注

        return { table: tableList, time, note };
      }
      catch (err) {
        throw err;
      }
    }
    return { table: {}, time: [], note: [] };
  }

  /**
   * * 班级课表查询
   * @param {string} term 学期
   * @param {string} yx 院系
   * @param {string} nj 年级
   * @param {string} zy 专业
   * @param {string} name 班级名称
   * @return {Array} *
   */
  async Ctable(term, yx, nj, zy, name) {
    console.log('name', name);
    const Cookie = this.ctx.state.Cookie;
    const apiUrl = `${this.ctx.state.baseUrl}/jsxsd/kbcx/kbxx_xzb_ifr`;
    const res = await this.ctx.curl(apiUrl, {
      method: 'POST',
      data: {
        xnxqh: term,
        kbjcmsid: '94673FF3230E4769E0533C41FF0A2703',
        skyx: yx,
        sknj: nj,
        skzy: zy,
        skbjid: '',
        skbj: '',
        zc1: '',
        zc2: '',
        skxq1: '',
        skxq2: '',
        jc1: '',
        jc2: '',
      },
      headers: {
        Cookie,
        'Content-Type': 'application/x-www-form-urlencoded',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
      },
      timeout: 10000,
    });
    if (res.status === 200) {
      try {
        const tableList = tableToList(res.data.toString(), '#kbtable');
        return tableList;
      } catch (err) {
        throw err;
      }
    }
    return [];
  }

  /**
   * * 教师课表查询
   * @param {string} term 学期
   * @param {string} yx 院系
   * @param {string} name 教师姓名
   * @return {Array} *
   */
  async Ttable(term, yx, name) {
    const Cookie = this.ctx.state.Cookie;
    const apiUrl = `${this.ctx.state.baseUrl}/jsxsd/kbcx/kbxx_teacher_ifr`;
    const res = await this.ctx.curl(apiUrl, {
      method: 'POST',
      data: {
        xnxqh: term,
        kbjcmsid: '94673FF3230E4769E0533C41FF0A2703',
        skyx: yx,
        jszc: '',
        skbjid: '',
        skjs: name,
        zc1: '',
        zc2: '',
        skxq1: '',
        skxq2: '',
        jc1: '',
        jc2: '',
      },
      headers: {
        Cookie,
        'Content-Type': 'application/x-www-form-urlencoded',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
      },
      timeout: 10000,
    });
    if (res.status === 200) {
      try {
        const tableList = tableToList(res.data.toString(), '#kbtable');
        return tableList;
      } catch (err) {
        throw err;
      }
    }
    return [];
  }

  /**
   * * 教室课表查询
   * @param {string} term 学期
   * @param {string} building 教学楼
   * @param {string} name 教室名称
   * @return {Array} *
   */
  async Rtable(term, building, name) {
    const Cookie = this.ctx.state.Cookie;
    const apiUrl = `${this.ctx.state.baseUrl}/jsxsd/kbcx/kbxx_classroom_ifr`;
    const res = await this.ctx.curl(apiUrl, {
      method: 'POST',
      data: {
        xnxqh: term,
        kbjcmsid: '94673FF3230E4769E0533C41FF0A2703',
        skyx: '',
        xqid: '',
        jzwid: building,
        gnq: '',
        jszc: '',
        skbjid: '',
        skjs: name,
        zc1: '',
        zc2: '',
        skxq1: '',
        skxq2: '',
        jc1: '',
        jc2: '',
      },
      headers: {
        Cookie,
        'Content-Type': 'application/x-www-form-urlencoded',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
      },
      timeout: 10000,
    });
    if (res.status === 200) {
      try {
        const tableList = tableToList(res.data.toString(), '#kbtable');
        return tableList;
      } catch (err) {
        throw err;
      }
    }
    return [];
  }

};
